package elitech.vietnam.myfashion.view;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Locale;
import java.util.Timer;
import java.util.TimerTask;

import org.xmlpull.v1.XmlPullParserException;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.koushikdutta.urlimageviewhelper.UrlImageViewHelper;

import elitech.vietnam.myfashion.DownloadImageTask;
import elitech.vietnam.myfashion.ImageViewActivity;
import elitech.vietnam.myfashion.LoginActivity;
import elitech.vietnam.myfashion.MainActivity;
import elitech.vietnam.myfashion.R;
import elitech.vietnam.myfashion.config.Const;
import elitech.vietnam.myfashion.dialog.OkCancelWarnningDialog;
import elitech.vietnam.myfashion.dialog.ShippingDialog;
import elitech.vietnam.myfashion.dialog.WarningDialog;
import elitech.vietnam.myfashion.doinbackground.CallServiceTask;
import elitech.vietnam.myfashion.entites.Color;
import elitech.vietnam.myfashion.entites.Comment;
import elitech.vietnam.myfashion.entites.Order;
import elitech.vietnam.myfashion.entites.OrderDetail;
import elitech.vietnam.myfashion.entites.Product;
import elitech.vietnam.myfashion.entites.ProductDetail;
import elitech.vietnam.myfashion.entites.Size;
import elitech.vietnam.myfashion.widgets.ScaleImageView;
import elitech.vietnam.myfashion.wsclient.Utilities;

public class ProductDetailView extends LinearLayout implements OnClickListener {

	private MainActivity mContext;
	private LinearLayout mLayoutSubImage, mLayoutComments, mLayoutColor,
			mLayoutSize, mLayoutLikes, mLayoutDetail;
	private Button mBtnBuy, mBtnAdd, mBtnSend;
	private ImageView mImage;
	private TextView mTxtName, mTxtPrice, mTxtSaleOff, mTxtContent,
			mTxtNumberComment, mTxtLike;
	private EditText mEdQuantity, mEdComment;
	private ProgressDialog mLoadingDialog;
	private ArrayList<String> mColorNameList = new ArrayList<String>();
	private ArrayList<String> mSizeNameList = new ArrayList<String>();
	private ArrayList<Color> mColorList = new ArrayList<Color>();
	private ArrayList<Size> mSizeList = new ArrayList<Size>();
	private Spinner mSpinColor, mSpinSize;
	private ArrayAdapter<String> mAdapterColor, mAdapterSize;
	private LayoutParams mLayoutParams;

	private boolean isLoading = false;
	private boolean isProcessing = false;
	private boolean isAdding = false;
	private Product mProduct;
	private int mProductID = -1;
	private String mTitle;

	public ProductDetailView(MainActivity context, Product product) {
		super(context);
		this.mContext = context;
		this.mProduct = product;

		LoadLayout();
	}
	
	public ProductDetailView(MainActivity context, int product) {
		super(context);
		this.mContext = context;
		this.mProductID = product;

		LoadLayout();
	}
	
	@Override
	protected void onDetachedFromWindow() {
		super.onDetachedFromWindow();
	}
	
	@Override
	protected void onAttachedToWindow() {
		super.onAttachedToWindow();
//		mContext.mBtnCart.setVisibility(View.VISIBLE);
	}
	
	private void GetDetail() {
		new CallServiceTask() {
			private ArrayList<ProductDetail> rs = new ArrayList<ProductDetail>();

			public void work() {
				try {
					rs = service.getProductDetail(mProduct.Id);
				} catch (IOException e) {
				} catch (XmlPullParserException e) {
				}
			};

			protected void onPostExecute(Void result) {
				mProduct.DetailList.addAll(rs);
				if(mProduct.DetailList.size()!=0){
					mLayoutDetail.setVisibility(View.VISIBLE);
					LoadDetail();
				}
			};
		}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
	}

	private void LoadDetail() {
		for (int i = 0; i < mProduct.DetailList.size(); i++) {
			final ProductDetail detail = mProduct.DetailList.get(i);
			ScaleImageView image = new ScaleImageView(getContext()){
				@Override
				protected void onDetachedFromWindow() {
					super.onDetachedFromWindow();
					Drawable drawable = getDrawable();
					if (drawable != null) {
						Bitmap bm = ((BitmapDrawable) drawable).getBitmap();
						if (bm != null && !bm.isRecycled()) {
							bm.recycle();
							bm = null;
							drawable = null;
						}
					}
				}
			};
			image.setAdjustViewBounds(true);
			image.setLayoutParams(mLayoutParams);
			image.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					Intent i = new Intent(mContext, ImageViewActivity.class);
					i.putExtra("Link", detail.Image);
					mContext.startActivity(i);
				}
			});
			
//			UrlImageViewHelper.setUrlDrawable(image, Const.SERVER_IMAGE_URL + detail.Image, R.drawable.loading300);
			new DownloadImageTask(mContext, image, false, 2).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, detail.Image);
			
			mLayoutSubImage.addView(image);
			if (detail.Content != null && detail.Content != "") {
				TextView txtText = new TextView(mContext);
				txtText.setText(detail.Content.replace("<br>", "\n"));
				txtText.setLayoutParams(new LayoutParams(
						LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
				txtText.setGravity(Gravity.CENTER);
				txtText.setPadding(8, 8, 8, 8);
				mLayoutSubImage.addView(txtText);
			}
		}
	}

	private void GetComment() {
		new CallServiceTask() {
			private ArrayList<Comment> rs = new ArrayList<Comment>();

			public void work() {
				try {
					rs = service.getCommentOfProduct(mProduct.Id, 1);
				} catch (IOException e) {
				} catch (XmlPullParserException e) {
				}
			};

			protected void onPostExecute(Void result) {
				mProduct.Comments = rs.size();
				mTxtNumberComment.setText(mProduct.Comments + "");
				for (int i = mLayoutComments.getChildCount(); i < rs.size(); i++) {
					CommentView comment = new CommentView(mContext, rs.get(i));
					mLayoutComments.addView(comment);
				}
			};
		}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
	}

	private void RefreshComment() {
		new CallServiceTask() {
			private ArrayList<Comment> rs = new ArrayList<Comment>();

			public void work() {
				try {
					rs = service.getCommentOfProduct(mProduct.Id, 1);
				} catch (IOException e) {
				} catch (XmlPullParserException e) {
				}
			};

			protected void onPostExecute(Void result) {
				mProduct.Comments = rs.size();
				mTxtNumberComment.setText(mProduct.Comments + "");
				for (int i = mLayoutComments.getChildCount(); i < rs.size(); i++) {
					CommentView comment = new CommentView(mContext, rs.get(i));
					mLayoutComments.addView(comment);
				}
			};
		}.execute();
	}

	private void GetSize() {
		new CallServiceTask() {
			private ArrayList<Size> rs = new ArrayList<Size>();

			public void work() {
				try {
					rs = service.getProductSize(mProduct.Id);
				} catch (IOException e) {
				} catch (XmlPullParserException e) {
				}
			};

			protected void onPostExecute(Void result) {
				mBtnBuy.setText(R.string.buyitnow);
				mBtnAdd.setText(R.string.addtocart);
				isLoading = false;
				mProduct.SizeList.addAll(rs);
				LoadSize();
			};
		}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
	}

	private void LoadSize() {
		if (mProduct.SizeList.size() != 0) {
			mSizeNameList.clear();
			mSizeList.clear();
			mSizeList.addAll(mProduct.SizeList);
			for (Size size : mProduct.SizeList) {
				mSizeNameList.add(size.Size);
			}
			mAdapterSize.notifyDataSetChanged();
			mLayoutSize.setVisibility(View.VISIBLE);
		}
	}

	private void GetColor() {
		new CallServiceTask() {
			private ArrayList<Color> rs = new ArrayList<Color>();

			protected void onPreExecute() {
				mBtnBuy.setText(R.string.process);
				mBtnAdd.setText(R.string.process);
				isLoading = true;
			};

			public void work() {
				try {
					rs = service.getProductColor(mProduct.Id);
				} catch (IOException e) {
				} catch (XmlPullParserException e) {
				}
			};

			protected void onPostExecute(Void result) {
				mProduct.ColorList.addAll(rs);
				LoadColor();
			};
		}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
	}

	private void LoadColor() {
		if (mProduct.ColorList.size() != 0) {
			mColorNameList.clear();
			mColorList.clear();
			mColorList.addAll(mProduct.ColorList);
			for (Color color : mProduct.ColorList) {
				
				if (mContext.getLocale().equals(Locale.ENGLISH))
					mColorNameList.add(color.NameEN);
				else if (mContext.getLocale().equals(Locale.KOREA))
					mColorNameList.add(color.NameKR);
				else
					mColorNameList.add(color.NameVN);
			}
			mLayoutColor.setVisibility(View.VISIBLE);
			mAdapterColor.notifyDataSetChanged();
		}
	}

	private void NewComment() {
		if (!isProcessing) {
			new CallServiceTask() {
				int rs = 0;

				protected void onPreExecute() {
					isProcessing = true;
				};

				public void work() {
					try {
						rs = service.newComment(mProduct.Id, mContext.mUser.Id,
								mEdComment.getText().toString(), 1);
					} catch (IOException e) {
					} catch (XmlPullParserException e) {
					}
				};

				protected void onPostExecute(Void result) {
					isProcessing = false;
					if (rs > 0) {
						GetComment();
						mEdComment.setText("");
						InputMethodManager inputManager = (InputMethodManager) mContext
								.getSystemService(Context.INPUT_METHOD_SERVICE);
						inputManager.hideSoftInputFromWindow(mContext
								.getCurrentFocus().getWindowToken(),
								InputMethodManager.HIDE_NOT_ALWAYS);
						NotifyComment(rs);
					}
				};
			}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
		}
	}
	
	private void NotifyComment(final int commentID) {
		new CallServiceTask() {
			public void work() {
				try {
					service.notifyComment(commentID, 1);
				} catch (IOException e) {
					e.printStackTrace();
				} catch (XmlPullParserException e) {
					e.printStackTrace();
				}
			};
		}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
	}

	private void checkQuantity() {
		if (mEdQuantity.getText().length() == 0) {
			mEdQuantity.setText("1");
		} else if (Integer.parseInt(mEdQuantity.getText().toString()) < 1) {
			mEdQuantity.setText("1");
		}
	}

	private void AddToCart(OrderDetail order) {
		for (OrderDetail detail : mContext.mOrder.ListDetail) {
			if (detail.ProductID == order.ProductID
					&& detail.Size == order.Size && detail.Color == order.Color) {
				detail.Quantity = detail.Quantity + order.Quantity;
				return;
			}
		}

		mContext.mOrder.ListDetail.add(order);

	}

	@Override
	public void onClick(final View v) {
		switch (v.getId()) {
		case R.id.detail_btnAdd:
			if (isLoading) {
				new WarningDialog(mContext,
						mContext.getString(R.string.processwait)).show();
				return;
			}
			if (!isAdding) {
				isAdding = true;
				checkQuantity();
				if (mContext.mUser != null) {
					if (mLayoutColor.getVisibility() == View.VISIBLE)
						mProduct.Color = mColorList.get(mSpinColor
								.getSelectedItemPosition());
					if (mLayoutSize.getVisibility() == View.VISIBLE)
						mProduct.Size = mSizeList.get(mSpinSize
								.getSelectedItemPosition());
					mProduct.Quantity = Integer.parseInt(mEdQuantity.getText()
							.toString());

					OrderDetail detail = new OrderDetail();
					detail.ProductID = mProduct.Id;
					detail.ProductName = mProduct.Name;
					detail.CategoryNameEN = mProduct.CategoryNameEN;
					detail.CategoryNameVN = mProduct.CategoryNameVN;
					detail.CategoryNameKR = mProduct.CategoryNameKR;
					detail.Price = mProduct.Price - mProduct.Price
							* mProduct.SaleOff / 100;
					detail.PriceVN = mProduct.PriceVN - mProduct.PriceVN
							* mProduct.SaleOff / 100;
					if (mProduct.Color != null) {
						detail.ColorNameEN = mProduct.Color.NameEN;
						detail.ColorNameKR = mProduct.Color.NameKR;
						detail.ColorNameVN = mProduct.Color.NameVN;
						detail.Color = mProduct.Color.ColorID;
					} else {
						detail.Color = -1;
					}
					if (mProduct.Size != null) {
						detail.SizeName = mProduct.Size.Size;
						detail.Size = mProduct.Size.SizeID;
					} else {
						detail.Size = -1;
					}
					detail.Quantity = mProduct.Quantity;
					detail.Image = mProduct.Image;
					detail.SaleOff = mProduct.SaleOff;

					AddToCart(detail);
				}
				Toast.makeText(mContext,
						mContext.getString(R.string.addsuccess),
						Toast.LENGTH_SHORT).show();
				isAdding = false;
			}

			break;
		case R.id.detail_btnBuy:
			v.setEnabled(false);
			if (isLoading) {
				new WarningDialog(mContext,
						mContext.getString(R.string.processwait)).show();
				return;
			}

			checkQuantity();
			if (mContext.mUser != null) {
				if (mLayoutColor.getVisibility() == View.VISIBLE)
					mProduct.Color = mColorList.get(mSpinColor
							.getSelectedItemPosition());
				if (mLayoutSize.getVisibility() == View.VISIBLE)
					mProduct.Size = mSizeList.get(mSpinSize
							.getSelectedItemPosition());
				mProduct.Quantity = Integer.parseInt(mEdQuantity.getText()
						.toString());

				OrderDetail detail = new OrderDetail();
				detail.ProductID = mProduct.Id;
				detail.ProductName = mProduct.Name;
				detail.CategoryNameEN = mProduct.CategoryNameEN;
				detail.CategoryNameVN = mProduct.CategoryNameVN;
				detail.CategoryNameKR = mProduct.CategoryNameKR;
				detail.Price = mProduct.Price - mProduct.Price
						* mProduct.SaleOff / 100;
				detail.PriceVN = mProduct.PriceVN - mProduct.PriceVN
						* mProduct.SaleOff / 100;
				if (mProduct.Color != null) {
					detail.CategoryNameEN = mProduct.CategoryNameEN;
					detail.CategoryNameVN = mProduct.CategoryNameVN;
					detail.CategoryNameKR = mProduct.CategoryNameKR;
					detail.Color = mProduct.Color.ColorID;
				} else {
					detail.Color = -1;
				}
				if (mProduct.Size != null) {
					detail.SizeName = mProduct.Size.Size;
					detail.Size = mProduct.Size.SizeID;
				} else {
					detail.Size = -1;
				}
				detail.Quantity = mProduct.Quantity;
				detail.Image = mProduct.Image;
				detail.SaleOff = mProduct.SaleOff;
				mContext.mBuyNowOrder = new Order();
				mContext.mBuyNowOrder.ListDetail.add(detail);

				ShippingDialog dialog = new ShippingDialog(mContext, true);
				dialog.setCancelable(true);
				dialog.show();
				dialog.setOnDismissListener(new OnDismissListener() {
					
					@Override
					public void onDismiss(DialogInterface dialog) {
						v.setEnabled(true);
					}
				});
			} else {
				Intent i = new Intent(mContext, LoginActivity.class);
				mContext.startActivityForResult(i, MainActivity.LOGINACTIVITY);
				v.setEnabled(true);
			}
			break;
		case R.id.detail_btnSend:
			if (mEdComment.getText().length() != 0
					&& Utilities.checkComment(mEdComment.getText().toString())) {
				if (mContext.mUser == null) {
					Intent intent = new Intent(mContext, LoginActivity.class);
					mContext.startActivityForResult(intent,
							MainActivity.LOGINACTIVITY);
				} else
					NewComment();
			} else {
				mEdComment.setText("");
				new WarningDialog(mContext,
						mContext.getString(R.string.commentblank)).show();
			}
			break;

		case R.id.detail_layBtnLike:
			if (!isProcessing) {
				if (mContext.mUser == null) {
					new OkCancelWarnningDialog(mContext, "You must login") {
						public void onOk() {
							Intent i = new Intent(mContext, LoginActivity.class);
							mContext.startActivityForResult(i,
									MainActivity.LOGINACTIVITY);
						};
					}.show();
				} else {
					if (mProduct.Liked()) {
						new OkCancelWarnningDialog(mContext,
								"Do you want to unlike this product?") {
							public void onOk() {
								new CallServiceTask() {
									private int rs = -2;

									protected void onPreExecute() {
										isProcessing = true;
									};

									public void work() {
										try {
											rs = service.newLike(
													mContext.mUser.Id,
													mProduct.Id,
													mProduct.Liked(), 1);
										} catch (IOException e) {
											// TODO Auto-generated catch
											// block
											e.printStackTrace();
										} catch (XmlPullParserException e) {
											// TODO Auto-generated catch
											// block
											e.printStackTrace();
										}
									};

									protected void onPostExecute(Void result) {
										if (rs == -2) {
											new WarningDialog(
													mContext,
													mContext.getString(R.string.connecterror))
													.show();
										} else if (rs == -1) {
											mProduct.Likes = mProduct.Likes - 1;
											mTxtLike.setText(mProduct.Likes
													+ "");
											mLayoutLikes
													.setBackgroundResource(R.drawable.like);
											mProduct.Liked = 0;
										} else {
											mTxtLike.setText(rs + "");
											mProduct.Likes = rs;
											mLayoutLikes
													.setBackgroundResource(R.drawable.like);
											mProduct.Liked = 0;
										}
										isProcessing = false;
									};
								}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
							};
						}.show();
					} else {
						new CallServiceTask() {
							private int rs = -2;

							protected void onPreExecute() {
								isProcessing = true;
							};

							public void work() {
								try {
									rs = service.newLike(mContext.mUser.Id,
											mProduct.Id, mProduct.Liked(), 1);
								} catch (IOException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								} catch (XmlPullParserException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
							};

							protected void onPostExecute(Void result) {
								if (rs == -2) {
									new WarningDialog(
											mContext,
											mContext.getString(R.string.connecterror))
											.show();
								} else if (rs == -1) {
									mProduct.Likes = mProduct.Likes + 1;
									mTxtLike.setText(mProduct.Likes + "");
									mProduct.Liked = 1;
									mLayoutLikes
											.setBackgroundResource(R.drawable.liked);
								} else {
									mTxtLike.setText(rs + "");
									mProduct.Likes = rs;
									mLayoutLikes
											.setBackgroundResource(R.drawable.liked);
									mProduct.Liked = 1;
								}
								isProcessing = false;
							};
						}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
					}
				}
			}
			break;

		}

	}

	private void LoadLayout() {
		new CallServiceTask() {
			public void work() {
				int accountID = (mContext.mUser==null) ? -1 : mContext.mUser.Id;
				if (mProduct == null)
					try {
						mProduct = service.getProductByID(mProductID, accountID);
					} catch (IOException e) {
						e.printStackTrace();
					} catch (XmlPullParserException e) {
						e.printStackTrace();
					}
			};
			protected void onPostExecute(Void result) {
				if (mProduct == null) {
					mContext.onBackPressed();
					return;
				}
				if (mContext.getLocale().equals(Locale.ENGLISH))
					mTitle = mProduct.CategoryNameEN;
				else if (mContext.getLocale().equals(Locale.KOREA))
					mTitle = mProduct.CategoryNameKR;
				else
					mTitle = mProduct.CategoryNameVN;

//				if (!mContext.mTxtTitle.getText().toString().equals(mTitle))
//					mContext.changeTitle(mTitle, true);
				
				LayoutInflater inflater = (LayoutInflater) mContext
						.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				if (inflater != null) {
					inflater.inflate(R.layout.product_detail, ProductDetailView.this);
				}
				
				mLayoutParams = new LayoutParams(LayoutParams.MATCH_PARENT,
						LayoutParams.WRAP_CONTENT);
				mLayoutParams.setMargins(0, 12, 0, 0);

				mImage = (ImageView) findViewById(R.id.detail_imgProduct);
				mTxtName = (TextView) findViewById(R.id.detail_txtCode);
				mTxtPrice = (TextView) findViewById(R.id.detail_txtPrice);
				mTxtContent = (TextView) findViewById(R.id.detail_txtContent);
				mTxtNumberComment = (TextView) findViewById(R.id.detail_txtNumberComment);
				mTxtSaleOff = (TextView) findViewById(R.id.detail_txtSaleOff);
				mTxtLike = (TextView) findViewById(R.id.detail_txtLikes);

				mEdQuantity = (EditText) findViewById(R.id.detail_txtQuantity);
				mEdComment = (EditText) findViewById(R.id.detail_txtComment);

				mLayoutColor = (LinearLayout) findViewById(R.id.detail_layColor);
				mLayoutSize = (LinearLayout) findViewById(R.id.detail_laySize);
				mLayoutSubImage = (LinearLayout) findViewById(R.id.detail_layoutSubImage);
				mLayoutComments = (LinearLayout) findViewById(R.id.detail_layoutComment);
				mLayoutLikes = (LinearLayout) findViewById(R.id.detail_layBtnLike);
				mLayoutDetail = (LinearLayout) findViewById(R.id.detail_layoutDetail);
				mLayoutSubImage = (LinearLayout) findViewById(R.id.detail_layoutSubImage);
				mLayoutComments = (LinearLayout) findViewById(R.id.detail_layoutComment);
				mSpinColor = (Spinner) findViewById(R.id.detail_spinnerColor);
				mSpinSize = (Spinner) findViewById(R.id.detail_spinnerSize);
				mBtnBuy = (Button) findViewById(R.id.detail_btnBuy);
				mBtnAdd = (Button) findViewById(R.id.detail_btnAdd);
				mBtnSend = (Button) findViewById(R.id.detail_btnSend);

				mImage.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						Intent i = new Intent(mContext, ImageViewActivity.class);
						i.putExtra("Link", mProduct.Image);
						mContext.startActivity(i);
					}
				});
				
//				new DownloadImageTask(mContext, mImage, true).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, mProduct.Image);
				UrlImageViewHelper.setUrlDrawable(mImage, Const.SERVER_IMAGE_THUMB_URL + mProduct.Image, R.drawable.loading300);

				mBtnBuy.setOnClickListener(ProductDetailView.this);
				mBtnAdd.setOnClickListener(ProductDetailView.this);
				mBtnSend.setOnClickListener(ProductDetailView.this);
				mLayoutLikes.setOnClickListener(ProductDetailView.this);

				mAdapterColor = new ArrayAdapter<String>(mContext,
						android.R.layout.simple_spinner_item, mColorNameList);
				mAdapterColor
						.setDropDownViewResource(android.R.layout.simple_list_item_single_choice);
				mSpinColor.setAdapter(mAdapterColor);

				mAdapterSize = new ArrayAdapter<String>(mContext,
						android.R.layout.simple_spinner_item, mSizeNameList);
				mAdapterSize
						.setDropDownViewResource(android.R.layout.simple_list_item_single_choice);
				mSpinSize.setAdapter(mAdapterSize);

				mEdQuantity.setOnFocusChangeListener(new OnFocusChangeListener() {

					@Override
					public void onFocusChange(View v, boolean hasFocus) {
						if (!hasFocus) {
							checkQuantity();
						}
					}
				});

				mLoadingDialog = new ProgressDialog(mContext);
				mLoadingDialog.setCancelable(false);
				mLoadingDialog.setMessage(mContext.getString(R.string.loading));

				// mContext.changeTitle(mProduct.CategoryNameEN);
				mTxtName.setText(mProduct.Name);
				mTxtPrice.setText(Utilities.numberFormat(mProduct.PriceVN) + " VND");
				mTxtLike.setText(mProduct.Likes + "");
				if (mProduct.Liked())
					mLayoutLikes.setBackgroundResource(R.drawable.liked);

				String content = "";
				if (mProduct.Content != null)
					content = mProduct.Content.replace("<br>", "\n");
				mTxtContent.setText(content);
				mTxtContent.setGravity(Gravity.CENTER);
				
				if (mProduct.SaleOff > 0) {
					mTxtSaleOff.setText("-" + mProduct.SaleOff + "%");
				}

				mTxtNumberComment.setText(mProduct.Comments + "");

				if (mProduct.ColorList.size() == 0)
					GetColor();
				else
					LoadColor();

				if (mProduct.SizeList.size() == 0)
					GetSize();
				else
					LoadSize();

				if (mProduct.DetailList.size() == 0)
					GetDetail();
				else
					LoadDetail();

				GetComment();
				Timer timer = new Timer();
				timer.schedule(new TimerTask() {

					@Override
					public void run() {
						RefreshComment();
					}
				}, 15000, 15000);
			};
		}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
	}
}
